//
//  CategoryDetailViewController.swift
//  exam2
//
//  Created by stu on 2019/10/16.
//  Copyright © 2019年 stu. All rights reserved.
//

import UIKit
protocol CategoryDetailViewControllerDelegate: class {
    func categoryDetailViewController(_ controller: CategoryDetailViewController,disFinishAdding category: TodoCategory)

func categoryDetailViewController(_ controller: CategoryDetailViewController,disFinishEditing category: TodoCategory)
}

class CategoryDetailViewController: UITableViewController, UITextFieldDelegate, IconPickerViewControllerDelegate{
    func iconPicker(_ picker: IconPickerViewController, didPick iconName: String) {
        self.imageName = iconName
        iconimage.image = UIImage(named: imageName)
        
        navigationController?.popViewController(animated: true)
    }
    
    
    weak var delegate: CategoryDetailViewControllerDelegate?
    
    var categoryToEdit: TodoCategory?
    var imageName: String = "Folder"

    @IBOutlet weak var doneBarbutton: UIBarButtonItem!
    
    @IBOutlet weak var inputTextField: UITextField!
    
    @IBOutlet weak var iconimage: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if let category = categoryToEdit {
            title = "重命名"
            inputTextField.text = category.name
            self.imageName = category.iconImage
        }else {
            title = "新增分类"
        }
        iconimage.image = UIImage(named: imageName)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        inputTextField.becomeFirstResponder()
    }

    func textField(_ textField: UITextField, shouldchangeCharactersIn range: NSRange, replacementString string: String) ->Bool {
        let oldText = textField.text! as NSString
        let newText = oldText.replacingCharacters(in: range, with: string)
        doneBarbutton.isEnabled = newText.count > 0
        return true
    }
    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        if indexPath.section == 1 {
            return indexPath
        }else{
            return nil
        }
    }
    
    
    @IBAction func done(_ sender: Any) {
        if let category = categoryToEdit {
            category.name = inputTextField.text!
            
            category.iconImage = imageName
            delegate?.categoryDetailViewController(self, disFinishEditing: category)
        }else {
            let category = TodoCategory(name: inputTextField.text!)
             category.iconImage = imageName
            delegate?.categoryDetailViewController(self, disFinishAdding: category)
        }
        
        
        
    }
    
    @IBAction func cancel(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "PickIcon" {
            let controller = segue.destination as! IconPickerViewController
            controller.delegate = self
        }
    }
}
